package com.explorx.sns.puma.im.core.chat.impl;


import com.alibaba.fastjson2.JSON;
import com.explorex.puma.common.rpc.RpcResult;
import com.explorex.puma.common.rpc.RpcResultUtils;
import com.explorx.sns.puma.im.core.chat.command.MessageCommand;
import com.explorx.sns.puma.im.api.ImSendService;
import com.explorx.sns.puma.im.api.dto.SendImMsgReq;
import com.explorx.sns.puma.im.core.chat.message.ChatMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@DubboService(version = "1.0.0")
@Service
public class ImSendServiceImpl implements ImSendService {

    @Resource
    private List<MessageCommand> messageCommands;

    @Override
    public RpcResult sendMessage(SendImMsgReq req) {
        try {
            for (MessageCommand messageCommand : messageCommands) {
                final ChatMessage chatMessage = JSON.parseObject(new String(req.getMessage()), ChatMessage.class);
                System.out.println(new String(req.getMessage()));
                if (messageCommand.cmd() == chatMessage.getCmd()) {
                    messageCommand.execute(chatMessage);
                }
            }
        } catch (Exception e) {
            log.error("sendMessage req={}",req,e);
        } finally {

        }
        return RpcResultUtils.success();
    }

}
